﻿using AutoMapper;
using Ezpay.ServiceContract;
using Microsoft.Practices.Prism;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Ezpay.UI.ViewModels
{
    class CustomerViewModel : BaseViewModel, INavigationAware
    {
        private IEzpayService _ezpayService;
        private IRegionManager _regionManager;
        public ICommand EditCommand { get; set; }
        public CustomerViewModel(IRegionManager regionManager, IEzpayService ezpayService)
        {
            _ezpayService = ezpayService;
            _regionManager = regionManager;
            EditCommand = new DelegateCommand(Edit);

            Transactions = new List<Transaction>()
                {
                    new Transaction(){BatchNumber = "1111", Description = "Ezpay Aug 2011", Credit = CreditDebit.Debit, Date = new DateTime(2011, 8, 1), Amount = 5000},
                    new Transaction(){BatchNumber = "2222", Description = "Ezpay Seg 2011", Credit = CreditDebit.Debit, Date = new DateTime(2011, 9, 1), Amount = 5000},
                    new Transaction(){BatchNumber = "3333", Description = "Ezpay Refund", Credit = CreditDebit.Credit, Date = new DateTime(2011, 9, 3), Amount = 200}
                };

            Subscriptions = new []
                {
                    new Subscription() {Description = "Okinawa Gas", Type = "Gas"},
                    new Subscription() {Description = "Okinawa Power", Type = "Electricity"},
                    new Subscription() {Description = "Okinawa City Water", Type = "Water"},
                    new Subscription() {Description = "Okinawa ISP", Type = "ISP"},
                }.Select(x => new SubscriptionViewModel(x, _regionManager)).ToList();

            Invoices = new List<Invoice>()
                {
                    new Invoice(){InvoiceNumber = "111111", Date = new DateTime(2011, 8, 1), Amount = 5000},
                    new Invoice(){InvoiceNumber = "222222", Date = new DateTime(2011, 9, 1), Amount = 5000},
                };
        }

        

        

        private string _customerID;
        public string CustomerID
        {
            get { return _customerID; }
            set
            {
                _customerID = value;
                RaisePropertyChanged(() => CustomerID);
            }
        }


        private string _firstName;
        public string FirstName
        {
            get { return _firstName; }
            set
            {
                _firstName = value;
                RaisePropertyChanged(() => FirstName);
            }
        }

        private string _lastName;
        public string LastName
        {
            get { return _lastName; }
            set
            {
                _lastName = value;
                RaisePropertyChanged(() => LastName);
            }
        }

        private string _middleInitial;
        public string MiddleInitial
        {
            get { return _middleInitial; }
            set
            {
                _middleInitial = value;
                RaisePropertyChanged(() => MiddleInitial);
            }
        }

        private string _cellPhone;
        public string CellPhone
        {
            get { return _cellPhone; }
            set
            {
                _cellPhone = value;
                RaisePropertyChanged(() => CellPhone);
            }
        }

        private string _primaryEmail;
        public string PrimaryEmail
        {
            get { return _primaryEmail; }
            set
            {
                _primaryEmail = value;
                RaisePropertyChanged(() => PrimaryEmail);
            }
        }

        public ICollection<Transaction> Transactions { get; private set; }
        public ICollection<SubscriptionViewModel> Subscriptions { get; private set; }

        public ICollection<Invoice> Invoices { get; private set; }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return navigationContext.Parameters["ID"] == CustomerID;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            if (IsNavigationTarget(navigationContext))
                return;

            navigationContext.NavigationService.Region.Remove(this);
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            var customerId = navigationContext.Parameters["ID"];

            LoadCustomer(customerId);
        }

        private async void LoadCustomer(string customerID)
        {
            var customer = await Task.Factory.StartNew(() =>
            {
                return _ezpayService.GetCustomer(new GetCustomerRequest() { CustomerID = customerID });
            });

            Mapper.Map(customer, this);
        }

        private void Edit()
        {
            var query = new UriQuery();
            query.Add("ID", CustomerID);
            _regionManager.ShowInMain("EditCustomerViewModel" + query.ToString());
        }
    }
    
}
